package com.project.weimi.adapter;

import java.util.List;

import com.project.weimi.R;
import com.project.weimi.entity.response.IntegralRecordEntity;
import com.project.weimi.entity.response.IntegralRecordEntity.IntegralRecord;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

public class IntegralRecordAdapter extends RecyclerView.Adapter<ViewHolder>{
	
	private Context context;
	private List<IntegralRecord> list;
	
	private static final int TYPE_ITEM = 0;
	private static final int TYPE_FOOTER = 1;
	
	public IntegralRecordAdapter(Context context, List<IntegralRecord> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getItemCount() {
		return list.size() >= 10 ? list.size() + 1 : list.size();
	}

	@Override
	public int getItemViewType(int position) {
		if(position + 1 == getItemCount() && getItemCount() >= 10){
			return TYPE_FOOTER;
		} else {
			return TYPE_ITEM;
		}
	}

	@Override
	public void onBindViewHolder(ViewHolder arg0, int arg1) {
		if(arg0 instanceof ItemViewHolder){
			((ItemViewHolder)arg0).tv_remark.setText(list.get(arg1).getRemark() + arg1);
			((ItemViewHolder)arg0).tv_createTime.setText(list.get(arg1).getCreateTime());
			((ItemViewHolder)arg0).tv_integrate.setText(list.get(arg1).getIntegrate());
		}
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
		if(arg1 == TYPE_ITEM){
			View view = LayoutInflater.from(context).inflate(R.layout.integral_record_recyclerview_item, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new ItemViewHolder(view);
		} else if (arg1 == TYPE_FOOTER) {
			View view = LayoutInflater.from(context).inflate(R.layout.shopping_footerview, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return new FooterViewHolder(view);
		}
		
		return null;
	}

	class FooterViewHolder extends ViewHolder{

		public FooterViewHolder(View itemView) {
			super(itemView);
		}
	}
	
	class ItemViewHolder extends ViewHolder {

		TextView tv_remark;
		TextView tv_createTime;
		TextView tv_integrate;
		
		public ItemViewHolder(View itemView) {
			super(itemView);
			tv_remark = (TextView)itemView.findViewById(R.id.tv_remark);
			tv_createTime = (TextView)itemView.findViewById(R.id.tv_createTime);
			tv_integrate = (TextView)itemView.findViewById(R.id.tv_integrate);
		}
	}
	
}
